package com.javabasic.algorithm.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author mir.xiong
 * @version 1.0
 * @description
 * @see
 * [1078. Occurrences After Bigram](https://leetcode-cn.com/problems/occurrences-after-bigram/)
 * @since Created by work on 2021/12/26 11:20 下午
 */
public class OccurrencesAfterBigram {

    /**
     * 暴力法
     * @param text
     * @param first
     * @param second
     * @return
     */
    public String[] findOcurrences(String text, String first, String second) {
        String[] words = text.split("\\s+");
        String[] result;
        List<String> resultStr = new ArrayList<>();
        int len = words.length;
        for (int i = 0; i < len-2; i++) {
            if (words[i].equals(first) && words[i+1].equals(second)) {
                resultStr.add(words[i+2]);
            }
        }
        len = resultStr.size();
        result = new String[len];
        int index = 0;
        for (int i = 0; i < len; i++) {
            result[index++] = resultStr.get(i);
        }
        return result;
    }



    public String[] findOcurrences2(String text, String first, String second) {
        String[] words = text.split(" ");
        String[] result;
        List<String> resultStr = new ArrayList<>();
        int len = words.length;
        for (int i = 0; i < len-2; i++) {
            if (words[i].equals(first) && words[i+1].equals(second)) {
                resultStr.add(words[i+2]);
            }
        }
        len = resultStr.size();
        result = new String[len];
        int index = 0;
        for (int i = 0; i < len; i++) {
            result[index++] = resultStr.get(i);
        }
        return result;
    }
}
